﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess.baobiaoDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;
using dhDataAcess.produceLinq;
using dhDataAcess.produceDAL;
using dhDataAcess.sysDAL;

namespace dhSystemBLL.produceBLL
{
   public  class rlckBLL
    {
       private V_rlckDetailTableAdapter _V_rlckDetailAdapter = null;
       private V_rlckDetailTableAdapter V_rlckDetailMasterAdapter
        {
            get
            {
                if (_V_rlckDetailAdapter == null) _V_rlckDetailAdapter = new V_rlckDetailTableAdapter();
                return _V_rlckDetailAdapter;

            }
        }
        private rlckMasterTableAdapter _rlckMasterlAdapter = null;
        private rlckMasterTableAdapter rlckMasterAdapter
        {
            get
            {
                if (_rlckMasterlAdapter == null) _rlckMasterlAdapter = new rlckMasterTableAdapter();
                return _rlckMasterlAdapter;

            }
        }
        private rlckDetailTableAdapter _rlckDetailAdapter = null;
        private rlckDetailTableAdapter rlckDetailAdapter
        {
            get
            {
                if (_rlckDetailAdapter == null) _rlckDetailAdapter = new rlckDetailTableAdapter();
                return _rlckDetailAdapter;

            }
        }
        /// <summary>
        /// 查询领料出库主表
        /// </summary>
        /// <returns></returns>

        public purchase.rlckMasterDataTable getrlckMasterAll()
        {
            try
            {
                return rlckMasterAdapter.GetDatarlckMasterALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料出库主表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_rlckDetailDataTable getV_rlckDetailBystockNamelikeNo(string matName)
        {
            try
            {
                return V_rlckDetailMasterAdapter.GetDataV_rlckDetailBystockNamelikeNo(matName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料出库主表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_rlckDetailDataTable getV_rlckDetailBystockNamelike(string matName)
        {
            try
            {
                return V_rlckDetailMasterAdapter.GetDataV_rlckDetailBystockNamelike (matName);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料出库主表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_rlckDetailDataTable getV_rlckDetailBystockNameNo()
        {
            try
            {
                return V_rlckDetailMasterAdapter.GetDataV_rlckDetailBystockNameNo ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料出库主表
        /// </summary>
        /// <returns></returns>

        public baobiaoDataSet.V_rlckDetailDataTable getV_rlckDetailBystockName()
        {
            try
            {
                return V_rlckDetailMasterAdapter.GetDataV_rlckDetailBystockName ();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 查询领料出库从表
        /// </summary>
        /// <returns></returns>

        public purchase.rlckDetailDataTable getrlckDetailAll()
        {
            try
            {
                return rlckDetailAdapter.GetDatarlckDetailALL();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询领料出库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.rlckMasterDataTable getrlckMasterAllBybillNo(string billNo)
        {
            try
            {
                return rlckMasterAdapter.GetDatarlckMasterBybillNo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据领料单状态查询领料出库表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.rlckMasterDataTable getrlckMasterAllBybillstatus(string billstatus)
        {
            try
            {
                return rlckMasterAdapter.GetDatarlckMasterBybillstatus(billstatus);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据入库单号查询领料出库从表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>
        public purchase.rlckDetailDataTable getsrlckDetailAllBybillNo(string billNo)
        {
            try
            {
                return rlckDetailAdapter.GetDatarlckDetailBybillNo (billNo);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 修改领料出库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int rlckInsert(purchase.rlckMasterDataTable masterTable, purchase.rlckDetailDataTable pTable, string state, string billNo, string Date)
        {
            rlckDetailModel rlckDetailRow = new rlckDetailModel();
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                rlckMasterDAL rlckMaster = new rlckMasterDAL(tn, conn);
                rlckDetailDAL rlckDetail = new rlckDetailDAL(tn, conn);
                rlckMaster.rlckMasterInsertII (masterTable, pTable);
                //basListDAL basList = new basListDAL(tn, conn);
                //if (state == "新增")
                //{

                //    purchase.rlckMasterDataTable pdrkTable = getrlckMasterAllBybillNo (billNo);
                //    if (pdrkTable.Rows.Count == 0)
                //    {
                //        rlckMaster.rlckMasterInsert(row);
                //    }
                //    else
                //    {
                //        row.billNo = "R" + Date + "-" + basicSql.getOrderSerail("领料出库单号");
                //        rlckMaster.rlckMasterInsert(row);
                //    }

                //    //更新单据编号的过程 
                //    string orderSerial = basicSql.getOrderSerail("领料出库单号");
                //    basList.basListretbasNumUpdate((Convert.ToInt32(orderSerial) + 1).ToString(), "领料出库单号");
                //}
                //if (state == "编辑")
                //{
                //    rlckMaster.rlckMasterUpdate(row);
                //}


                //foreach (purchase.rlckDetailRow detailRow in pTable)
                //{
                //    if (detailRow.RowState != DataRowState.Deleted)
                //    {
                //        rlckDetailRow .recordid  = detailRow.recordid;
                //        rlckDetailRow.billNo = detailRow.billNo;
                //        rlckDetailRow.matNum  = detailRow.matNum ;
                //        rlckDetailRow.matName = detailRow.matName;
                //        if (!detailRow.IsmatSpecNull ())
                //        {
                //            rlckDetailRow.matSpec = detailRow.matSpec;
                //        }
                //        else
                //        {
                //            rlckDetailRow.matSpec = "";
                //        }
                //        rlckDetailRow.qty = detailRow.qty;
                //        rlckDetailRow.unit = detailRow.unit;
                //        rlckDetailRow.createDate = DateTime.Now;
                    
                //        if (!detailRow.IsmarkNull())
                //        {
                //            rlckDetailRow.mark = detailRow.mark;
                //        }
                //        else
                //        {
                //            rlckDetailRow.mark = "";
                //        }
                //    }

                //    //编辑子表记录,新增
                //    if (detailRow.RowState == DataRowState.Added)
                //    {

                //        rlckDetail.rlckDetailInsert(rlckDetailRow);


                //    }
                //    //修改
                //    if (detailRow.RowState == DataRowState.Modified)
                //    {
                //        rlckDetail.rlckDetailUpdate(rlckDetailRow);
                //    }
                //    //删除
                //    if (detailRow.RowState == DataRowState.Deleted)
                //    {
                //        DataRow delrow = (DataRow)detailRow;
                //        rlckDetail.rlckDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                //    }


                //}
                //foreach (orderDataSet .scrlDetailRow  detailRow in scrlpTable )
                //{
                //    if (detailRow.RowState != DataRowState.Deleted)
                //    {
                //        rlckDetailRow.recordid = detailRow.recordid;
                //        rlckDetailRow.billNo = detailRow.billNo;
                //        rlckDetailRow.matNum = detailRow.matNum;
                //        rlckDetailRow.matName = detailRow.matName;
                //        if (!detailRow.IsmatSpecNull())
                //        {
                //            rlckDetailRow.matSpec = detailRow.matSpec;
                //        }
                //        else
                //        {
                //            rlckDetailRow.matSpec = "";
                //        }
                //        rlckDetailRow.qty = detailRow.qty;
                //        rlckDetailRow.unit = detailRow.unit;
                //        rlckDetailRow.createDate = DateTime.Now;

                //        if (!detailRow.IsmarkNull())
                //        {
                //            rlckDetailRow.mark = detailRow.mark;
                //        }
                //        else
                //        {
                //            rlckDetailRow.mark = "";
                //        }
                //    }

                //    //编辑子表记录,新增
                //    if (detailRow.RowState == DataRowState.Added)
                //    {

                //        rlckDetail.rlckDetailInsert(rlckDetailRow);


                //    }
                //    //修改
                //    if (detailRow.RowState == DataRowState.Modified)
                //    {
                //        rlckDetail.rlckDetailUpdate(rlckDetailRow);
                //    }
                //    //删除
                //    if (detailRow.RowState == DataRowState.Deleted)
                //    {
                //        DataRow delrow = (DataRow)detailRow;
                //        rlckDetail.rlckDetailDel(delrow["recordid", DataRowVersion.Original].ToString());
                //    }


                //}

                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个入库单号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


        /// 删除一行领料出库表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public int rlckMasterDel(string billNo)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                rlckMasterDAL rlckMaster = new rlckMasterDAL(tn, conn);
                rlckMaster.rlckDel (billNo);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());

            }
            finally
            {
                conn.Close();
            }
        }


    }
}
